﻿using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FFSupplier.Common.Helper
{
    /// <summary>
    /// 日志帮助类
    /// by DZL 20190430
    /// </summary>
    public class LoggerHelper
    {
        #region 单例模式

        private LoggerHelper()
        {
        }

        private static readonly object LockObj = new object();

        private static LoggerHelper _instance;

        /// <summary>
        /// 获得对象实例
        /// </summary>
        public static LoggerHelper Instance
        {
            get
            {
                lock (LockObj)
                {
                    if (_instance == null)
                    {
                        _instance = new LoggerHelper();
                    }
                    return _instance;
                }
            }
        }

        public void WriteLog(string v1, object p, int v2)
        {
            throw new NotImplementedException();
        }

        #endregion 单例模式

        #region 属性

        private Logger _log;
        /// <summary>
        /// 日志实例
        /// </summary>
        public Logger Log
        {
            get
            {
                if (_log == null) _log = LogManager.GetCurrentClassLogger();
                return _log;
            }
            private set { _log = value; }
        }

        #region 自定义NLog事件属性

        private LogEventInfo _apiCallInfo;
        /// <summary>
        /// 默认 API 请求信息记录 
        /// </summary>
        public LogEventInfo APICallInfo
        {
            get
            {
                if (_apiCallInfo == null) _apiCallInfo = SetCustomInfo(LogLevel.Info, "APIInfoLogger", "收到一条请求");
                return _apiCallInfo;
            }
            private set { _apiCallInfo = value; }
        }


        #endregion 自定义NLog事件属性

        #endregion 属性

        #region 方法

        #region 普通方式
        public void Debug(string msg)
        {
            Log.Debug(msg);
        }
        public void Debug(string msg, params object[] args)
        {
            Log.Debug(msg, args);
        }
        public void Debug(string msg, Exception ex)
        {
            Log.Debug(ex, msg);
        }
        public void Warn(string msg)
        {
            Log.Warn(msg);
        }
        public void Warn(string msg, params object[] args)
        {
            Log.Warn(msg, args);
        }
        public void Warn(string msg, Exception ex)
        {
            Log.Warn(ex, msg);
        }
        public void Trace(string msg)
        {
            Log.Trace(msg);
        }
        public void Trace(string msg, params object[] args)
        {
            Log.Trace(msg, args);
        }
        public void Trace(string msg, Exception ex)
        {
            Log.Trace(ex, msg);
        }
        public void Fatal(string msg, params object[] args)
        {
            Log.Fatal(msg, args);
        }

        public void Fatal(string msg, Exception ex)
        {
            Log.Fatal(ex, msg);
        }
        #endregion 普通方式

        #region 错误日志
        /// <summary>
        /// 错误记录
        /// </summary>
        /// <param name="msg">方法名</param>
        /// <param name="ex">异常</param>
        public void Error(string msg, Exception ex)
        {
            LogEventInfo logInfo = SetCustomInfo(LogLevel.Error, "ExceptionLogger", msg);
            logInfo.Properties["ErrorHead"] = "程序发生错误：";
            logInfo.Exception = ex;
            Log.Log(LogLevel.Error, logInfo);
        }
        #endregion 错误日志

        #region 报文信息记录
        /// <summary>
        /// 记录来访请求报文
        /// </summary>
        /// <param name="msg">报文信息</param>
        public void CreateRequestRecord(string msg)
        {
            LogEventInfo logInfo = SetCustomInfo(LogLevel.Info, "WebAPILogger", msg);
            logInfo.Properties["WebAPIHead"] = "收到一条API请求";
            logInfo.Properties["WebAPIEnd"] = "  请求结束";
            Log.Log(LogLevel.Info, logInfo);
        }
        /// <summary>
        /// 记录其他系统访问我方接口请求报文
        /// </summary>
        /// <param name="methodmes">业务模块</param>
        /// <param name="datastr">报文信息(数据)/响应信息</param>
        /// <param name="type">请求记录类型 0:请求记录 1：响应记录</param>
        public void WriteLog(string methodmes, string datastr, int type = 0)
        {
            StringBuilder sb = new StringBuilder();
            if (type == 0)
            {
                string msg = string.Format("请求业务模块：{0},报文信息如下：", methodmes);
                sb.AppendLine(msg);
                sb.AppendLine("报文数据:" + datastr + "");
                LogEventInfo logInfo = SetCustomInfo(LogLevel.Info, "WebAPILogger", sb.ToString());
                logInfo.Properties["WebAPIHead"] = "接收一次API请求";
                logInfo.Properties["WebAPIEnd"] = "  请求结束";
                Log.Log(LogLevel.Info, logInfo);
            }
            else
            {
                string msg = string.Format("响应请求业务模块：{0},报文信息如下：", methodmes);
                sb.AppendLine(msg);
                sb.AppendLine("响应数据:" + datastr + "");
                LogEventInfo logInfo = SetCustomInfo(LogLevel.Info, "WebAPILogger", sb.ToString());
                logInfo.Properties["WebAPIHead"] = "API请求响应记录";
                logInfo.Properties["WebAPIEnd"] = "  API请求响应记录结束";
                Log.Log(LogLevel.Info, logInfo);
            }

        }
        /// <summary>
        /// 记录访问其他系统请求报文
        /// </summary>
        /// <param name="methodmes">类名与方法名</param>
        /// <param name="url">请求地址</param>
        /// <param name="datastr">报文信息(数据)/响应信息</param>
        public void WriteRequest(string methodmes, string datastr, string url = "")
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(url))
            {
                string msg = string.Format("发起请求位置：{0},报文信息如下：", methodmes);
                sb.AppendLine(msg);
                sb.AppendLine("请求地址：" + url);
                sb.AppendLine("报文数据:" + datastr + "");
                LogEventInfo logInfo = SetCustomInfo(LogLevel.Info, "WebAPILogger", sb.ToString());
                logInfo.Properties["WebAPIHead"] = "发起一次API请求";
                logInfo.Properties["WebAPIEnd"] = "  请求结束";
                Log.Log(LogLevel.Info, logInfo);
            }
            else
            {
                string msg = string.Format("响应请求位置：{0},报文信息如下：", methodmes);
                sb.AppendLine(msg);
                sb.AppendLine("响应数据:" + datastr + "");
                LogEventInfo logInfo = SetCustomInfo(LogLevel.Info, "WebAPILogger", sb.ToString());
                logInfo.Properties["WebAPIHead"] = "API请求响应记录";
                logInfo.Properties["WebAPIEnd"] = "  API请求响应记录结束";
                Log.Log(LogLevel.Info, logInfo);
            }
        }
        #endregion 报文信息记录

        #region 私有方法
        /// <summary>
        /// 设置自定义日志事件
        /// </summary>
        /// <param name="level"></param>
        /// <param name="loggerName"></param>
        /// <param name="message"></param>
        /// <param name="customPropertie"></param>
        /// <param name="customPropertieValue"></param>
        /// <returns></returns>
        private LogEventInfo SetCustomInfo(LogLevel level, string loggerName, string message, string customPropertie = "", string customPropertieValue = "")
        {
            LogEventInfo ei = new LogEventInfo(level, loggerName, message); //也可以用LogEventInfo.Create(level, loggerName, message);
            if (!string.IsNullOrEmpty(customPropertie) && !string.IsNullOrEmpty(customPropertieValue))
                ei.Properties[customPropertie] = customPropertieValue;
            return ei;

        }

        //private void AddPropertieToLogEventInfo(LogEventInfo logEventntInfo, string customPropertie, string customPropertieValue)
        //{
        //    logEventntInfo.Properties[customPropertie] = customPropertieValue;
        //}
        #endregion 私有方法

        #endregion 方法
    }
}
